import { TweenPlugin } from "./TweenPlugin";
import { TweenLite } from "../TweenLite";
//class MotionBlurPlugin
    
    export  class MotionBlurPlugin extends TweenPlugin
    {
        constructor()
        {
            
            super();this._blur = new Object /* flash.filters.BlurFilter */(0, 0, 2);
            this._matrix = new Object /* flash.geom.Matrix */();
            this.propName = "motionBlur";
            this.overwriteProps = ["motionBlur"];
            this.onComplete = this.disable;
            this.onDisable = this.onTweenDisable;
            this.priority = -1;
            this.activeDisable = true;
            return;
        }

        public /* override */ onInitTween(arg1: Object, arg2, arg3: TweenLite): Boolean
        {
            var target: Object;
            var value;
            var tween: TweenLite;
            var x: number;
            var y: number;

            var loc1;
            x = NaN;
            y = NaN;
            target = arg1;
            value = arg2;
            tween = arg3;
            if (!(target is flash.display.DisplayObject)) 
            {
                throw new Error("motionBlur tweens only work for DisplayObjects.");
            }
            if (value != false) 
            {
                if (typeof value == "object") 
                {
                    this._strength = (value.strength || 1) * 0.05;
                    this._blur.quality = int(value.quality) || 2;
                }
            }
            else 
            {
                this._strength = 0;
            }
            if (!MotionBlurPlugin._classInitted) 
            {
                try 
                {
                    MotionBlurPlugin._isFlex = Boolean(flash.utils.getDefinitionByName("mx.managers.SystemManager"));
                }
                catch (e)
                {
                    MotionBlurPlugin._isFlex = false;
                }
                MotionBlurPlugin._classInitted = true;
            }
            this._target = target as Object /* flash.display.DisplayObject */;
            this._tween = tween;
            this._time = 0;
            this._xRef = loc2 = this._target.x;
            this._xCurrent = loc2;
            this._yRef = loc2 = this._target.y;
            this._yCurrent = loc2;
            this._alpha = this._target.alpha;
            if ("x" in this._tween.propTweenLookup && "y" in this._tween.propTweenLookup && !this._tween.propTweenLookup.x.isPlugin && !this._tween.propTweenLookup.y.isPlugin) 
            {
                this._angle = 180 - Math.atan2(this._tween.propTweenLookup.y.change, this._tween.propTweenLookup.x.change) * MotionBlurPlugin._RAD2DEG;
            }
            else if (!(this._tween.vars.x == null) || !(this._tween.vars.y == null)) 
            {
                x = this._tween.vars.x || this._target.x;
                y = this._tween.vars.y || this._target.y;
                this._angle = 180 - Math.atan2(y - this._target.y, x - this._target.x) * MotionBlurPlugin._RAD2DEG;
            }
            else 
            {
                this._angle = 0;
            }
            this._bd = new Object /* flash.display.BitmapData */(this._target.width + 30, this._target.height + 30, true, 16777215);
            this._bitmap = new Object /* flash.display.Bitmap */(this._bd);
            this._bitmap.smoothing = Boolean(this._blur.quality > 1);
            this._sprite = MotionBlurPlugin._isFlex ? new (flash.utils.getDefinitionByName("mx.core.UIComponent"))() : new Object /* flash.display.Sprite */();
            this._sprite.mouseChildren = loc2 = false;
            this._sprite.mouseEnabled = loc2;
            this._sprite.addChild(this._bitmap);
            this._rect = new Object /* flash.geom.Rectangle */(0, 0, this._bd.width, this._bd.height);
            if (this._target.mask) 
            {
                this._mask = this._target.mask;
            }
            return true;
        }

        /* internal  */disable(): void
        {
            if (this._strength != 0) 
            {
                this._target.alpha = this._alpha;
            }
            if (this._sprite.parent) 
            {
                this._sprite.parent.removeChild(this._sprite);
            }
            if (this._mask) 
            {
                this._target.mask = this._mask;
            }
            return;
        }

        /* internal  */onTweenDisable(): void
        {
            if (_changeFactor != 1) 
            {
                this.disable();
            }
            return;
        }

        public /* override */ set changeFactor(arg1: number)
        {
            var loc1=this._target.x - this._xCurrent;
            var loc2=this._target.y - this._yCurrent;
            var loc3=this._target.x - this._xRef;
            var loc4=this._target.y - this._yRef;
            _changeFactor = arg1;
            if (loc3 > 2 || loc4 > 2 || loc3 < -2 || loc4 < -2) 
            {
                this._angle = 180 - Math.atan2(loc2, loc1) * MotionBlurPlugin._RAD2DEG;
                this._xRef = this._target.x;
                this._yRef = this._target.y;
            }
            var loc5;
            if ((loc5 = this._tween.cachedTime - this._time) < 0) 
            {
                loc5 = -loc5;
            }
            this._blur.blurX = Math.sqrt(loc1 * loc1 + loc2 * loc2) * this._strength / loc5;
            this._xCurrent = this._target.x;
            this._yCurrent = this._target.y;
            this._time = this._tween.cachedTime;
            if (this._blur.blurX < 1 || this._target.parent == null || arg1 == 0) 
            {
                if (!(this._sprite.parent == null) || arg1 == 0) 
                {
                    this._xRef = this._target.x;
                    this._yRef = this._target.y;
                    this.disable();
                }
                return;
            }
            if (!(this._sprite.parent == this._target.parent) && this._target.parent) 
            {
                this._target.parent.addChildAt(this._sprite, this._target.parent.getChildIndex(this._target));
                if (this._mask) 
                {
                    this._sprite.mask = this._mask;
                }
            }
            var loc7;
            this._target.y = loc7 = 20000;
            this._target.x = loc7;
            this._target.rotation = this._target.rotation + this._angle;
            var loc6;
            if ((loc6 = this._target.getBounds(this._target.parent)).width + this._blur.blurX * 2 > 2870) 
            {
                this._blur.blurX = loc6.width >= 2870 ? 0 : (2870 - loc6.width) * 0.5;
            }
            if (loc6.height > this._bd.height || loc6.width + this._blur.blurX * 2 > this._bd.width) 
            {
                this._bitmap.bitmapData = loc7 = new Object /* flash.display.BitmapData */(loc6.width + this._blur.blurX * 2 + 10, loc6.height + 10, true, 16777215);
                this._bd = loc7;
                this._rect = new Object /* flash.geom.Rectangle */(0, 0, this._bd.width, this._bd.height);
                this._bitmap.smoothing = Boolean(this._blur.quality > 1);
            }
            this._matrix.tx = this._blur.blurX - loc6.x;
            this._matrix.ty = -loc6.y;
            this._bitmap.x = loc6.x - this._target.x - this._blur.blurX;
            this._bitmap.y = loc6.y - this._target.y;
            loc6.y = loc7 = 0;
            loc6.x = loc7;
            loc6.width = loc6.width + this._blur.blurX * 2;
            if (this._target.alpha != 0.00390625) 
            {
                this._alpha = this._target.alpha;
            }
            else 
            {
                this._target.alpha = this._alpha;
            }
            this._bd.fillRect(this._rect, 16777215);
            this._bd.draw(this._target.parent, this._matrix, MotionBlurPlugin._ct, "normal", loc6, Boolean(this._blur.quality > 1));
            this._bd.applyFilter(this._bd, loc6, MotionBlurPlugin._point, this._blur);
            this._sprite.rotation = -this._angle;
            this._target.rotation = this._target.rotation - this._angle;
            this._sprite.x = loc7 = this._xCurrent;
            this._target.x = loc7;
            this._sprite.y = loc7 = this._yCurrent;
            this._target.y = loc7;
            this._target.alpha = 0.00390625;
            return;
        }

        public static /* const */API: number=1;

        private static /* const */_DEG2RAD: number=Math.PI / 180;

        private static /* const */_RAD2DEG: number=180 / Math.PI;

        private static /* const */_point: Object /* flash.geom.Point */=new Object /* flash.geom.Point */(0, 0);

        private static /* const */_ct: Object /* flash.geom.ColorTransform */=new Object /* flash.geom.ColorTransform */();

        protected /* var */_target: Object /* flash.display.DisplayObject */;

        protected /* var */_time: number;

        protected /* var */_xCurrent: number;

        protected /* var */_yCurrent: number;

        protected /* var */_bd: Object /* flash.display.BitmapData */;

        protected /* var */_bitmap: Object /* flash.display.Bitmap */;

        protected /* var */_strength: number=0.05;

        protected /* var */_tween: TweenLite;

        protected /* var */_blur: Object /* flash.filters.BlurFilter */;

        protected /* var */_matrix: Object /* flash.geom.Matrix */;

        protected /* var */_sprite: Object /* flash.display.Sprite */;

        protected /* var */_rect: Object /* flash.geom.Rectangle */;

        protected /* var */_angle: number;

        protected /* var */_alpha: number;

        protected /* var */_xRef: number;

        protected /* var */_yRef: number;

        protected /* var */_mask: Object /* flash.display.DisplayObject */;

        private static /* var */_classInitted: Boolean;

        private static /* var */_isFlex: Boolean;
    }
